package com.chtw.oasystem.controller;

import com.chtw.oasystem.bean.Approve;
import com.chtw.oasystem.common.response.RespUtils;
import com.chtw.oasystem.respBean.ApproveRespBean;
import com.chtw.oasystem.service.impl.ApproveServiceImpl;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * @author chtw
 * @date 2020-11-25 23:41
 */
@RestController
@ResponseBody
@RequestMapping("/approve")
public class ApproveController {

    @Autowired
    private ApproveServiceImpl approveService;

    private static final Logger logger = LoggerFactory.getLogger(ApproveController.class);

    @GetMapping("/create")
    public RespUtils<Boolean> create(HttpSession session, int recordsId, String file_name) {
        try {
            return RespUtils.success(approveService.create(session, recordsId, file_name));
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RespUtils.fail(e.getMessage());
        }
    }

    @GetMapping("/op")
    public RespUtils<Boolean> op(HttpSession session, int id, String status, String desc) {
        try {
            return RespUtils.success(approveService.approve(session, id, status, desc));
        } catch (Exception e) {
            return RespUtils.fail(e.getMessage());
        }
    }

    @GetMapping("/list")
    public RespUtils<ApproveRespBean> list(@RequestParam(defaultValue = "1", required = false) int page, @RequestParam(defaultValue = "10", required = false) int limit, HttpSession session){
        try{
            PageInfo<Approve> info = approveService.list(session, page, limit);
            return RespUtils.success(new ApproveRespBean((int) info.getTotal(), info.getPages(), info.getList()));
        }catch (Exception e){
            logger.error(e.getMessage());
            return RespUtils.fail(e.getMessage());
        }
    }
}
